package com.hdoit.kylin.logical.group.service.impl;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.hdoit.kylin.logical.group.dao.IProjectDao;
import com.hdoit.kylin.logical.group.model.Project;
import com.hdoit.kylin.logical.group.service.IProjectService;
import com.hdoit.kylin.logical.relative.model.ProjectUser;
import com.hdoit.kylin.logical.relative.service.IProjectUserService;
import com.hdoit.kylin.logical.viewmodel.project.ProjectSearch;

/**
* @author luozhicheng
* @email
* @createtime 2017年07月19日 16:04:20
* @comment
*/
@Component
public class ProjectServiceImpl extends SuperService<Project> implements IProjectService{
	private IProjectDao projectDao;
	private IProjectUserService projectUserService;
	
	public IProjectUserService getProjectUserService() {
		return projectUserService;
	}
	@Resource
	public void setProjectUserService(IProjectUserService projectUserService) {
		this.projectUserService = projectUserService;
	}

	public IProjectDao getProjectDao() {
		return projectDao;
	}

	@Resource
	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	
	@Override
	public int queryCountByMark(ProjectSearch search, Integer mark){
		return projectDao.queryCountByMark(search,mark);
	}
	
	@Override
	public List<Project> queryListByGroupId(String groupId){
		return projectDao.queryListByGroupId(groupId);
	}
	
	@Override
	public boolean applyChange(Project project){
		if(super.applyChange(project)){
			if(ModelStatus.update.equals(project.getModelStatus())){
				deleteProjectUser(project.getProjectId());
			}
			List<ProjectUser> projectUsers = getProjectUserList(project);
			if(projectUsers != null && projectUsers.size() > 0){
				return projectUserService.insertListEx(projectUsers)==projectUsers.size();
			}
			return true;
		}
		return false;
	}
	
	private List<ProjectUser> getProjectUserList(Project project) {
		List<ProjectUser> projectUsers = new ArrayList<ProjectUser>();
		String personIds = project.getPersonIds();
		ProjectUser projectUser = null;
		if(!StringUtils.isEmpty(personIds)){
			String[] userIds = personIds.split(",");
			for(String userId : userIds){
				projectUser = new ProjectUser();
				projectUser.setId(CommonUtils.getKeyId());
				projectUser.setEmployeeId(userId);
				projectUser.setProjectId(project.getProjectId());
				projectUser.setModelStatus(ModelStatus.add);
				
				projectUsers.add(projectUser);
			}
		}
		return projectUsers;
	}
	private boolean deleteProjectUser(String projectId) {
		return projectUserService.deleteProjectUserByProjectId(projectId);
	}

	@Override
	public List<Project> queryListByOwn(String loginId) {
		return projectDao.queryList(loginId);
	}

	@Override
	public List<Project> queryList(int mark,PagerModel pagerModel){
		return projectDao.queryList(mark, pagerModel);
	}

	
}
